'''

'''

from .conn_mysql import ConnectMysql

class ConsumptionNotify(ConnectMysql):
    '''该类有两个方法，
    def consumption_notify(self)是用来测试用户是否可以
    将对应的consumption_id添加关注；
    def add_consumpation(self)方法是用来测试并添加关注到数据库的方法
    '''

    def __init__(self, company_id, consumption_id):

        super().__init__()
        self.company_id = company_id
        self.consumption_id = consumption_id

        self.consumption_notify = self.consumption_notify()

    def __enter__(self):

        return self

    def __exit__(self, exc_type, exc_value, traceback):

        self.close()
        # print('关门')

#         print(f'type:{exc_type}')
#         print(f'value:{exc_value}')
#         print(f'trace:{traceback}')


#         return True#加上他就不抛出异常


    def consumption_notify(self):#只有测试，入库加测试见下面函数
        if self.company_id == None or self.consumption_id == None:
        
            return {
                'status': -1,
                'errorCode': 16,
                'errorMsg':'未提交必传参数',
                'resultBody':None
            }

        if self.company_id.isdecimal() and self.consumption_id.isdecimal():#检查数据是否合法


            sql = 'SELECT * FROM company WHERE id=%s'

            sql_consumption = 'SELECT * FROM consumption WHERE company_id=%s and consumption_id=%s'

            company_info = self.select_one(sql,(self.company_id,))

            consumption_info = self.select_one(sql,(self.consumption_id,))

            if company_info and consumption_info:#检查ID是否存在

                if company_info['consumption_surplus'] == 0:#检查用户是否有足够的查看次数


                    return {
                        'status': -1,
                        'errorCode': 12,
                        'errorMsg':f'{self.company_id},没有足够的查看次数,请冲值',
                        'resultBody':None
                    }

                elif self.select_one(sql_consumption,(self.company_id, self.consumption_id)):#检查用户是否已经付过费用
     
                    return {
                        'status': -1,
                        'errorCode': 13,
                        'errorMsg':f'亲！你已经关注了该信息',
                        'resultBody':None
                    }

                else:#可添加关注对像到自己的名下

                    return {
                        'status': 1,
                        'errorCode': None,
                        'errorMsg':None,
                        'resultBody':f'可以添加consumption_id：{self.consumption_id}到您的箩筐中'
                    }

            else:#检查ID是否存在



                return {
                    'status': -1,
                    'errorCode': 11,
                    'errorMsg':f'对应的id没有在库里找到,company_id:{self.company_id},consumption_id:{self.consumption_id}',
                    'resultBody':None
                }


        else:#非法参数



            return {
                'status': -1,
                'errorCode': 10,
                'errorMsg':f'请检查参数是否合法,company_id:{self.company_id},consumption_id:{self.consumption_id}',
                'resultBody':None
            }

    def add_consumpation(self):#入库加测试

        if self.consumption_notify['status'] == 1:


            sql_add_consumpation = 'INSERT INTO consumption(company_id, consumption_id) VALUES (%s,%s)'

            self.insert_info(sql_add_consumpation, (self.company_id, self.consumption_id))#插入数据

            self.update_info(
                "UPDATE company SET consumption_surplus=consumption_surplus-1 WHERE id=%s",

                (self.company_id,)
            )#减去一次查看次数

            return {
                'status': 1,
                'errorCode': None,
                'errorMsg':None,
                'resultBody':f'添加consumption_id：{self.consumption_id}到您的箩筐中'
            }

        return self.consumption_notify